package com.yic.module.device.controller.admin.inspectionPlanDevice;

import cn.hutool.core.util.ObjectUtil;
import com.yic.module.base.api.center.CenterApi;
import com.yic.module.base.api.center.dto.CenterRespDTO;
import com.yic.module.base.api.factory.FactoryApi;
import com.yic.module.base.api.factory.dto.FactoryRespDTO;
import com.yic.module.device.dal.dataobject.device.DeviceDO;
import com.yic.module.device.dal.dataobject.deviceCategory.DeviceCategoryDO;
import com.yic.module.device.service.device.DeviceService;
import com.yic.module.device.service.deviceCategory.DeviceCategoryService;
import com.yic.module.system.api.dept.DeptApi;
import com.yic.module.system.api.dept.dto.DeptRespDTO;
import com.yic.module.system.api.user.AdminUserApi;
import com.yic.module.system.api.user.dto.AdminUserRespDTO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.yic.framework.common.pojo.PageResult;
import com.yic.framework.common.pojo.CommonResult;
import static com.yic.framework.common.pojo.CommonResult.success;

import com.yic.framework.excel.core.util.ExcelUtils;

import com.yic.framework.operatelog.core.annotations.OperateLog;
import static com.yic.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.yic.module.device.controller.admin.inspectionPlanDevice.vo.*;
import com.yic.module.device.dal.dataobject.inspectionPlanDevice.InspectionPlanDeviceDO;
import com.yic.module.device.convert.inspectionPlanDevice.InspectionPlanDeviceConvert;
import com.yic.module.device.service.inspectionPlanDevice.InspectionPlanDeviceService;

@Tag(name = "管理后台 - 巡检计划与设备关联")
@RestController
@RequestMapping("/device/inspection-plan-device")
@Validated
public class InspectionPlanDeviceController {

    @Resource
    private InspectionPlanDeviceService inspectionPlanDeviceService;
    @Resource
    private DeviceService deviceService;
    @Resource
    private FactoryApi factoryApi;
    @Resource
    private DeviceCategoryService deviceCategoryService;
    @Resource
    private DeptApi deptApi;
    @Resource
    private AdminUserApi adminUserApi;
    @Resource
    private CenterApi centerApi;

    @PostMapping("/create")
    @Operation(summary = "创建巡检计划与设备关联")
    @PreAuthorize("@ss.hasPermission('device:inspection-plan-device:create')")
    public CommonResult<Long> createInspectionPlanDevice(@Valid @RequestBody InspectionPlanDeviceCreateReqVO createReqVO) {
        return success(inspectionPlanDeviceService.createInspectionPlanDevice(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新巡检计划与设备关联")
    @PreAuthorize("@ss.hasPermission('device:inspection-plan-device:update')")
    public CommonResult<Boolean> updateInspectionPlanDevice(@Valid @RequestBody InspectionPlanDeviceUpdateReqVO updateReqVO) {
        inspectionPlanDeviceService.updateInspectionPlanDevice(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除巡检计划与设备关联")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('device:inspection-plan-device:delete')")
    public CommonResult<Boolean> deleteInspectionPlanDevice(@RequestParam("id") Long id) {
        inspectionPlanDeviceService.deleteInspectionPlanDevice(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得巡检计划与设备关联")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('device:inspection-plan-device:query')")
    public CommonResult<InspectionPlanDeviceRespVO> getInspectionPlanDevice(@RequestParam("id") Long id) {
        InspectionPlanDeviceDO inspectionPlanDevice = inspectionPlanDeviceService.getInspectionPlanDevice(id);
        return success(InspectionPlanDeviceConvert.INSTANCE.convert(inspectionPlanDevice));
    }

    @GetMapping("/list")
    @Operation(summary = "获得巡检计划与设备关联列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('device:inspection-plan-device:query')")
    public CommonResult<List<InspectionPlanDeviceRespVO>> getInspectionPlanDeviceList(@RequestParam("ids") Collection<Long> ids) {
        List<InspectionPlanDeviceDO> list = inspectionPlanDeviceService.getInspectionPlanDeviceList(ids);
        return success(InspectionPlanDeviceConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得巡检计划与设备关联分页")
    @PreAuthorize("@ss.hasPermission('device:inspection-plan-device:query')")
    public CommonResult<PageResult<InspectionPlanDeviceRespVO>> getInspectionPlanDevicePage(@Valid InspectionPlanDevicePageReqVO pageVO) {
        PageResult<InspectionPlanDeviceDO> pageResult = inspectionPlanDeviceService.getInspectionPlanDevicePage(pageVO);
        return success(InspectionPlanDeviceConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/app-page")
    @Operation(summary = "获得移动端巡检计划与设备关联分页")
    @PreAuthorize("@ss.hasPermission('device:inspection-plan-device:query')")
    public CommonResult<PageResult<InspectionPlanDeviceRespVO>> getInspectionPlanDeviceAppPage(@Valid InspectionPlanDevicePageReqVO pageVO) {
        PageResult<InspectionPlanDeviceDO> pageResult = inspectionPlanDeviceService.getInspectionPlanDeviceAppPage(pageVO);
        PageResult<InspectionPlanDeviceRespVO> result = InspectionPlanDeviceConvert.INSTANCE.convertPage(pageResult);
        result.getList().forEach(deviceRespVO -> {
            DeviceDO deviceDO = deviceService.get(deviceRespVO.getDeviceId());
            if (ObjectUtil.isNotNull(deviceDO)) {
                deviceRespVO.setName(deviceDO.getName());
                deviceRespVO.setCode(deviceDO.getCode());
                deviceRespVO.setSpec(deviceDO.getSpec());
                deviceRespVO.setSn(deviceDO.getSn());
                deviceRespVO.setMac(deviceDO.getMac());

                // 封装设备分类
                DeviceCategoryDO category = deviceCategoryService.getCategory(deviceDO.getCategoryId());
                if (ObjectUtil.isNotEmpty(category)) {
                    deviceRespVO.setCategoryName(category.getName());
                }
                // 封装部门
                DeptRespDTO dept = deptApi.getDept(deviceDO.getDeptId());
                if (ObjectUtil.isNotEmpty(dept)) {
                    deviceRespVO.setDeptName(dept.getName());
                }
                // 封装工作中心
                CenterRespDTO center = centerApi.getCenter(deviceDO.getCenterId());
                if (ObjectUtil.isNotEmpty(center)) {
                    deviceRespVO.setCenterName(center.getName());
                }

                // 封装工厂
                FactoryRespDTO factory = factoryApi.getFactory(deviceDO.getFactoryId());
                if (ObjectUtil.isNotEmpty(factory)) {
                    deviceRespVO.setFactoryName(factory.getName());
                }

                //封装使用人
                AdminUserRespDTO user = adminUserApi.getUser(deviceDO.getUserId());
                if (ObjectUtil.isNotEmpty(user)) {
                    deviceRespVO.setUserName(user.getNickname());
                }
                // 封装管理员
                AdminUserRespDTO admin = adminUserApi.getUser(deviceDO.getAdminId());
                if (ObjectUtil.isNotEmpty(admin)) {
                    deviceRespVO.setAdminName(admin.getNickname());
                }
            }
        });
        return success(result);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出巡检计划与设备关联 Excel")
    @PreAuthorize("@ss.hasPermission('device:inspection-plan-device:export')")
    @OperateLog(type = EXPORT)
    public void exportInspectionPlanDeviceExcel(@Valid InspectionPlanDeviceExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<InspectionPlanDeviceDO> list = inspectionPlanDeviceService.getInspectionPlanDeviceList(exportReqVO);
        // 导出 Excel
        List<InspectionPlanDeviceExcelVO> datas = InspectionPlanDeviceConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "巡检计划与设备关联.xls", "数据", InspectionPlanDeviceExcelVO.class, datas);
    }

}
